package edu.cmu.ece.ds.droidchat.controller;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import edu.cmu.ece.ds.droidchat.R;
import edu.cmu.ece.ds.droidchat.model.ChatSessionInfo;
import edu.cmu.ece.ds.droidchat.model.UserInfo;
import edu.cmu.ece.ds.droidchat.util.Toolbox;
import edu.cmu.ece.ds.droidchat.widget.ActionItem;
import edu.cmu.ece.ds.droidchat.widget.MyQuickAction;
import edu.cmu.ece.ds.droidchat.widget.QuickActionList;
import edu.cmu.ece.ds.droidchat.widget.QuickActionList.OnListClickListener;
import greendroid.widget.QuickActionBar;
import greendroid.widget.QuickActionWidget;
import greendroid.widget.QuickActionWidget.OnQuickActionClickListener;

public class UserListBaseAdapter extends BaseAdapter {
    public final static int THEME_BLUE = 0;
    public final static int THEME_TRANSPARENT = 1;

    private static final String TAG = "UserListBaseAdapter";

    private final boolean useVerticalQuickAction = true;
    private ArrayList<UserInfo> mUserInfoArray;
    private LayoutInflater mInflater;
    private int mTheme;

    public UserListBaseAdapter(Context context,
            ArrayList<UserInfo> userInfoArray, int theme) {
        mUserInfoArray = userInfoArray;
        mInflater = LayoutInflater.from(context);
        mTheme = theme;
    }

    @Override
    public int getCount() {
        return mUserInfoArray.size();
    }

    @Override
    public Object getItem(int position) {
        return mUserInfoArray.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            if (mTheme == THEME_BLUE) {
                convertView = mInflater.inflate(
                        R.layout.nearby_users_list_item_blue, null);
            } else if (mTheme == THEME_TRANSPARENT) {
                convertView = mInflater.inflate(
                        R.layout.nearby_users_list_item_transparent, null);
            } else {
                Log.e(TAG, "No such theme!");
            }
            holder = new ViewHolder();
            holder.iconImageView = (ImageView) convertView
                    .findViewById(R.id.user_image);
            holder.nicknameTextView = (TextView) convertView
                    .findViewById(R.id.user_nickname);
            holder.statusTextView = (TextView) convertView
                    .findViewById(R.id.user_status);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final UserInfo uInfo = mUserInfoArray.get(position);
        if (uInfo.getProfilePicture() == null) {
            Toolbox.imageDownloader.downloadToUserInfo(
                    uInfo.getProfilePictureURLString(), uInfo,
                    holder.iconImageView);
        } else {
            holder.iconImageView.setImageDrawable(uInfo.getProfilePicture());
        }
        final ImageView cView = holder.iconImageView;
        if (mTheme != THEME_TRANSPARENT) {
            if (!useVerticalQuickAction) {
                holder.iconImageView.setOnClickListener(new OnClickListener() {
                    private QuickActionBar actionBar;

                    @Override
                    public void onClick(View v) {
                        actionBar = new QuickActionBar(cView.getContext(),
                                R.drawable.action_bar_front_layer_background);

                        actionBar.addQuickAction(new MyQuickAction(cView
                                .getContext(), R.drawable.new_session,
                                R.string.create_new_session));
                        /*
                         * actionBar.addQuickAction(new MyQuickAction(cView
                         * .getContext(), R.drawable.invite,
                         * R.string.invite_to_existing_session));
                         * actionBar.addQuickAction(new MyQuickAction(cView
                         * .getContext(), R.drawable.info, R.string.info));
                         * actionBar.addQuickAction(new MyQuickAction(cView
                         * .getContext(), R.drawable.block, R.string.block));
                         */
                        actionBar
                                .setOnQuickActionClickListener(new UserLogoOnQuickActionClickListener(
                                        uInfo));

                        holder.iconImageView
                                .setOnClickListener(new OnClickListener() {

                                    @Override
                                    public void onClick(View v) {
                                        actionBar.show(cView);
                                    }
                                });

                    }
                });
            } else {
                // useVerticalQuickAction == true

                cView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        QuickActionList l = new QuickActionList(v,
                                QuickActionList.HORIZONTAL);

                        l.addActionItem(new ActionItem(cView.getContext(),
                                R.drawable.new_session,
                                R.string.create_new_session,
                                QuickActionList.VERTICAL));
                        /*
                         * l.addActionItem(new ActionItem(cView.getContext(),
                         * R.drawable.invite,
                         * R.string.invite_to_existing_session,
                         * QuickActionList.VERTICAL)); l.addActionItem(new
                         * ActionItem(cView.getContext(), R.drawable.info,
                         * R.string.info, QuickActionList.VERTICAL));
                         * l.addActionItem(new ActionItem(cView.getContext(),
                         * R.drawable.block, R.string.block,
                         * QuickActionList.VERTICAL));
                         */
                        l.setOnListClickListener(new VerticalListOnClickListener(
                                uInfo));

                        l.show();
                    }
                });
            }
        } else {
            // transparent theme used in ChatWindowActivity menu.
            holder.nicknameTextView.setFocusable(false);
            holder.statusTextView.setFocusable(false);
            holder.iconImageView.setFocusable(false);
        }
        holder.nicknameTextView.setText(uInfo.getNickName());
        holder.statusTextView.setText(uInfo.getStatusMessage());

        return convertView;
    }

    static class ViewHolder {
        ImageView iconImageView;
        TextView nicknameTextView;
        TextView statusTextView;
    }

    // These two listeners should be the same as they just corresponds to the
    // same behavior in two different modes (useVerticalQuickAction)
    private class UserLogoOnQuickActionClickListener implements
            OnQuickActionClickListener {

        private UserInfo mUserInfo;

        public UserLogoOnQuickActionClickListener(UserInfo uInfo) {
            mUserInfo = uInfo;
        }

        public void onQuickActionClicked(QuickActionWidget widget, int position) {
            switch (position) {
            case 0:
                /*
                 * Toast.makeText(widget.getContentView().getContext(),
                 * "new chat with " + mUserInfo.getNickName(),
                 * Toast.LENGTH_SHORT).show();
                 */
                String chatSessionId = Toolbox
                        .addNewOneOnOneChatSessionInfoByUsername(mUserInfo
                                .getUsername());
                ChatSessionInfo chInfo = Toolbox.getChatSession(chatSessionId);

                if (chInfo.isBrandNew()) {
                    System.out.println("is brandNew");
                    Toolbox.createChatWindowActivity(widget.getContentView()
                            .getContext(), true, chatSessionId);
                } else {
                    System.out.println("is NOT brandNew");
                    if (chInfo.getUsername() != null) {
                        Toast.makeText(
                                widget.getContentView().getContext(),
                                "you have invited \"" + mUserInfo.getUsername()
                                        + "\" for a 1-on-1 chat session",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(
                                widget.getContentView().getContext(),
                                "1-on-1 chat session with \""
                                        + mUserInfo.getUsername()
                                        + "\" has already been established",
                                Toast.LENGTH_SHORT).show();
                    }
                }

                /*
                 * old code
                 * Toolbox.createChatWindowActivity(widget.getContentView()
                 * .getContext(), true, chatSessionId);
                 */
                break;
            /*
             * case 1: Toast.makeText(widget.getContentView().getContext(),
             * "invite " + mUserInfo.getNickName(), Toast.LENGTH_SHORT) .show();
             * break; case 2:
             * Toast.makeText(widget.getContentView().getContext(),
             * "view info of " + mUserInfo.getNickName(),
             * Toast.LENGTH_SHORT).show(); break; case 3:
             * Toast.makeText(widget.getContentView().getContext(), "block " +
             * mUserInfo.getNickName(), Toast.LENGTH_SHORT) .show(); break;
             */
            }
        }
    }

    private class VerticalListOnClickListener implements OnListClickListener {
        private UserInfo mUserInfo;

        public VerticalListOnClickListener(UserInfo uInfo) {
            mUserInfo = uInfo;
        }

        @Override
        public void onQuickActionListClicked(View contentView, int position) {
            switch (position) {
            case 0:
                /*
                 * Toast.makeText(contentView.getContext(), "new chat with " +
                 * mUserInfo.getNickName(), Toast.LENGTH_SHORT).show();
                 */
                String chatSessionId = Toolbox
                        .addNewOneOnOneChatSessionInfoByUsername(mUserInfo
                                .getUsername());

                ChatSessionInfo chInfo = Toolbox.getChatSession(chatSessionId);

                if (chInfo.isBrandNew()) {
                    System.out.println("is brandNew");
                    Toolbox.createChatWindowActivity(contentView.getContext(),
                            true, chatSessionId);
                } else {
                    System.out.println("is NOT brandNew");
                    if (chInfo.getUsername() != null) {
                        Toast.makeText(
                                contentView.getContext(),
                                "you have invited \"" + mUserInfo.getUsername()
                                        + "\" for a 1-on-1 chat session",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(
                                contentView.getContext(),
                                "1-on-1 chat session with \""
                                        + mUserInfo.getUsername()
                                        + "\" has already been established",
                                Toast.LENGTH_SHORT).show();
                    }
                }
                /*
                 * Toolbox.createChatWindowActivity(contentView.getContext(),
                 * true, chatSessionId);
                 */
                break;
            /*
             * case 1: Toast.makeText(contentView.getContext(), "invite " +
             * mUserInfo.getNickName(), Toast.LENGTH_SHORT) .show(); break; case
             * 2: Toast.makeText(contentView.getContext(), "view info of " +
             * mUserInfo.getNickName(), Toast.LENGTH_SHORT).show(); break; case
             * 3: Toast.makeText(contentView.getContext(), "block " +
             * mUserInfo.getNickName(), Toast.LENGTH_SHORT) .show(); break;
             */
            }
        }
    }
}
